<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是shell——运算符和语句相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-02-shell-03-运算符和语句">
<meta property="og:url" content="https://sumumm.github.io/post/fbb9bfa7.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是shell——运算符和语句相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-03-%E8%BF%90%E7%AE%97%E7%AC%A6%E5%92%8C%E8%AF%AD%E5%8F%A5/img/image-20220214145142579.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-03-%E8%BF%90%E7%AE%97%E7%AC%A6%E5%92%8C%E8%AF%AD%E5%8F%A5/img/image-20220214145436032.png">
<meta property="article:published_time" content="2022-02-21T03:53:25.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.988Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV02-编译基础">
<meta property="article:tag" content="shell脚本">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-03-%E8%BF%90%E7%AE%97%E7%AC%A6%E5%92%8C%E8%AF%AD%E5%8F%A5/img/image-20220214145142579.png">


<link rel="canonical" href="https://sumumm.github.io/post/fbb9bfa7.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/fbb9bfa7.html","path":"post/fbb9bfa7.html","title":"LV02-02-shell-03-运算符和语句"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-02-shell-03-运算符和语句 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">一、运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">1.算术运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">2.关系运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%B8%83%E5%B0%94%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">3.布尔运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">4.逻辑运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">5.字符串运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%96%87%E4%BB%B6%E6%B5%8B%E8%AF%95%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-text">6.文件测试运算符</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E8%AF%B4%E6%98%8E%E6%80%A7%E8%AF%AD%E5%8F%A5"><span class="nav-text">二、说明性语句</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%8A%9F%E8%83%BD%E6%80%A7%E8%AF%AD%E5%8F%A5"><span class="nav-text">三、功能性语句</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-readonly"><span class="nav-text">1. readonly </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-unset"><span class="nav-text">2. unset </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-read"><span class="nav-text">3. read </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">3.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-test"><span class="nav-text">4. test </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1%E6%95%B0%E5%80%BC%E6%B5%8B%E8%AF%95"><span class="nav-text">4.1数值测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%B5%8B%E8%AF%95"><span class="nav-text">4.2字符串测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3%E6%96%87%E4%BB%B6%E6%B5%8B%E8%AF%95"><span class="nav-text">4.3文件测试</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-echo"><span class="nav-text">5. echo </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">5.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.2使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-1%E6%98%BE%E7%A4%BA%E6%99%AE%E9%80%9A%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">5.2.1显示普通字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-2%E6%98%BE%E7%A4%BA%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="nav-text">5.2.2显示转义字符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-3%E6%98%BE%E7%A4%BA%E5%8F%98%E9%87%8F"><span class="nav-text">5.2.3显示变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-4%E6%98%BE%E7%A4%BA%E6%8D%A2%E8%A1%8C%E4%B8%8E%E4%B8%8D%E6%8D%A2%E8%A1%8C"><span class="nav-text">5.2.4显示换行与不换行</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-5%E6%98%BE%E7%A4%BA%E7%BB%93%E6%9E%9C%E5%AE%9A%E5%90%91%E8%87%B3%E6%96%87%E4%BB%B6"><span class="nav-text">5.2.5显示结果定向至文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-6%E5%8E%9F%E6%A0%B7%E8%BE%93%E5%87%BA%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">5.2.6原样输出字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-7%E6%98%BE%E7%A4%BA%E5%91%BD%E4%BB%A4%E6%89%A7%E8%A1%8C%E7%BB%93%E6%9E%9C"><span class="nav-text">5.2.7显示命令执行结果</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-printf"><span class="nav-text">6. printf </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">6.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="nav-text">6.2 转义字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-3-%E6%A0%BC%E5%BC%8F%E6%8E%A7%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">6.3 格式控制字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#6-3-1%E6%A0%BC%E5%BC%8F%E6%8E%A7%E5%88%B6%E5%AD%97%E7%AC%A6"><span class="nav-text">6.3.1格式控制字符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-3-2%E6%A0%87%E8%AF%86"><span class="nav-text">6.3.2标识</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-3-3%E6%9C%80%E5%B0%8F%E8%BE%93%E5%87%BA%E5%AE%BD%E5%BA%A6%EF%BC%88-width-%EF%BC%89"><span class="nav-text">6.3.3最小输出宽度（ width ）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-3-4%E7%B2%BE%E5%BA%A6%EF%BC%88-precision-%EF%BC%89"><span class="nav-text">6.3.4精度（ .precision ）</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">6.4使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E7%BB%93%E6%9E%84%E6%80%A7%E8%AF%AD%E5%8F%A5"><span class="nav-text">四、结构性语句</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%88%86%E6%94%AF%E8%AF%AD%E5%8F%A5"><span class="nav-text">1.分支语句</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-if%E2%80%A6else%E2%80%A6-%E8%AF%AD%E5%8F%A5"><span class="nav-text">1.1 if…else… 语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F1"><span class="nav-text">1.1.1使用格式1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-2%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F2"><span class="nav-text">1.1.2使用格式2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-3%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F3"><span class="nav-text">1.1.3使用格式3</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-case-%E8%AF%AD%E5%8F%A5"><span class="nav-text">1.2 case 语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">1.2.1使用格式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2.2使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-text">2.循环语句</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-for-%E5%BE%AA%E7%8E%AF"><span class="nav-text">2.1 for 循环</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F1"><span class="nav-text">2.1.1使用格式1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F2"><span class="nav-text">2.1.2使用格式2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-3%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.1.3使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-while-%E5%BE%AA%E7%8E%AF"><span class="nav-text">2.2 while 循环</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F1"><span class="nav-text">2.2.1使用格式1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F2"><span class="nav-text">2.2.2使用格式2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-3%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2.3使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-until-%E5%BE%AA%E7%8E%AF"><span class="nav-text">2.3 until 循环</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">2.3.1使用格式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.3.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4%E6%97%A0%E9%99%90%E5%BE%AA%E7%8E%AF%E5%86%99%E6%B3%95"><span class="nav-text">2.4无限循环写法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-1-for"><span class="nav-text">2.4.1 for </span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-2-while"><span class="nav-text">2.4.2 while </span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5%E8%B7%B3%E5%87%BA%E5%BE%AA%E7%8E%AF"><span class="nav-text">2.5跳出循环</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">2.5.1使用格式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.5.2使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%B8%89%E7%9B%AE%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9F"><span class="nav-text">3.三目运算符？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F-1"><span class="nav-text">3.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81-Shell-%E4%B8%AD%E5%B8%A6%E9%A2%9C%E8%89%B2%E7%9A%84%E8%BE%93%E5%87%BA"><span class="nav-text">五、 Shell 中带颜色的输出</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-ANSI-%E6%8E%A7%E5%88%B6%E7%A0%81%E7%AE%80%E4%BB%8B"><span class="nav-text">1. ANSI 控制码简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-ANSI-%E6%8E%A7%E5%88%B6%E7%A0%81"><span class="nav-text">2. ANSI 控制码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-ANSI-%E7%A0%81%E5%BA%94%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">3. ANSI 码应用格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-echo"><span class="nav-text">4.1 echo </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-printf"><span class="nav-text">4.2 printf </span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-%E8%87%AA%E7%94%A8%E5%8F%98%E9%87%8F%E5%AE%9A%E4%B9%89"><span class="nav-text">4.3 自用变量定义</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/fbb9bfa7.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-02-shell-03-运算符和语句 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-02-shell-03-运算符和语句
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-02-21 11:53:25" itemprop="dateCreated datePublished" datetime="2022-02-21T11:53:25+08:00">2022-02-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV02-编译基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>8.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>32 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是shell——运算符和语句相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-03-%E8%BF%90%E7%AE%97%E7%AC%A6%E5%92%8C%E8%AF%AD%E5%8F%A5/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、运算符"><a href="#一、运算符" class="headerlink" title="一、运算符"></a><font size=3>一、运算符</font></h1><p>shell  支持很多运算符，包括<strong>算数运算符</strong>、<strong>关系运算符</strong>、<strong>布尔运算符</strong>、<strong>字符串运算符</strong>和<strong>文件测试运算符</strong>。原生 bash 不支持简单的数学运算，但是可以通过其他命令来实现，例如  awk  和  expr ， expr  最常用,它是一款表达式计算工具，使用它能完成表达式的求值操作。</p>
<h2 id="1-算术运算符"><a href="#1-算术运算符" class="headerlink" title="1.算术运算符"></a><font size=3>1.算术运算符</font></h2><table>
    <tr>
        <td align="center" width=100px>运算符</td>
        <td align="center">说明</td>
        <td align="center">实例(设变量a=6, b=3)</td>
    </tr>
    <tr>
        <td align="center" width=100px>+</td>
        <td align="center">加法</td>
        <td align="left"> expr $a + $b  结果为 9</td>
    </tr>
    <tr>
        <td align="center" width=100px>-</td>
        <td align="center">减法</td>
        <td align="left"> expr $a - $b  结果为 3</td>
    </tr>
    <tr>
        <td align="center" width=100px>*</td>
        <td align="center">乘法，注意要加 \</td>
        <td align="left"> expr $a \* $b  结果为 18</td>
    </tr>
    <tr>
        <td align="center" width=100px>/</td>
        <td align="center">除法</td>
        <td align="left"> expr $a / $b  结果为 2</td>
    </tr>
    <tr>
        <td align="center" width=100px>%</td>
        <td align="center">取余</td>
        <td align="left"> expr $b % $a  结果为 0</td>
    </tr>
    <tr>
        <td align="center" width=100px>=</td>
        <td align="center">赋值</td>
        <td align="left">a=$b 把变量 b 的值赋给 a</td>
    </tr>
    <tr>
        <td align="center" width=100px>==</td>
        <td align="center">相等，比较两个数字，相同则返回 true</td>
        <td align="left">[ $a == $b ] 返回 false</td>
    </tr>
    <tr>
        <td align="center" width=100px>!=</td>
        <td align="center">不相等，比较两个数字，不相同则返回 true</td>
        <td align="left">[ $a != $b ] 返回 true</td>
    </tr>
</table>



<p>【注意】</p>
<p>（1）<strong>表达式和运算符之间要有空格</strong>，例如  $a+$b  是不对的，必须写成  $a + $b 。</p>
<p>（2）使用 expr 时，完整的表达式要被  &#96;  包含。</p>
<p>（3）赋值操作时， &#x3D; 两端不能有空格。</p>
<p>（4）条件表达式要放在方括号之间，并且<strong>与方括号之间要有空格</strong>，例如: [$a&#x3D;&#x3D;$b] 是错误的，必须写成 [ $a &#x3D;&#x3D; $b ] 。</p>
<p>（5）乘号( * )前边必须加反斜杠( \ )才能实现乘法运算。</p>
<h2 id="2-关系运算符"><a href="#2-关系运算符" class="headerlink" title="2.关系运算符"></a><font size=3>2.关系运算符</font></h2><p>关系运算符只支持数字，不支持字符串，除非字符串的值是数字。</p>
<table>
    <tr>
        <td align="center" width=100px>运算符</td>
        <td align="center" colspan="2" width=200px>说明</td>
        <td align="center">实例(设变量a=6, b=3)</td>
    </tr>
    <tr>
        <td align="center" width=100px>-eq</td>
        <td align="center" width=100px>相等</td>
        <td align="center" width=100px>==</td>
        <td align="left">[ $a -eq $b ] 返回 false</td>
    </tr>
    <tr>
        <td align="center" width=100px>-ne</td>
        <td align="center" width=100px>不相等</td>
        <td align="center" width=100px>!=</td>
        <td align="left">[ $a -ne $b ] 返回 true</td>
    </tr>
    <tr>
        <td align="center" width=100px>-gt</td>
        <td align="center" width=100px>大于</td>
        <td align="center" width=100px>></td>
        <td align="left">[ $a -gt $b ] 返回 true</td>
    </tr>
    <tr>
        <td align="center" width=100px>-lt</td>
        <td align="center" width=100px>小于</td>
        <td align="center" width=100px><</td>
        <td align="left">[ $a -lt $b ] 返回 false</td>
    </tr>
    <tr>
        <td align="center" width=100px>-ge</td>
        <td align="center" width=100px>大于等于</td>
        <td align="center" width=100px>>=</td>
        <td align="left">[ $a -ge $b ] 返回 true</td>
    </tr>
    <tr>
        <td align="center" width=100px>-le</td>
        <td align="center" width=100px>小于等于</td>
        <td align="center" width=100px><=</td>
        <td align="left">[ $a -le $b ] 返回 false</td>
    </tr>
</table>



<h2 id="3-布尔运算符"><a href="#3-布尔运算符" class="headerlink" title="3.布尔运算符"></a><font size=3>3.布尔运算符</font></h2><table>
    <tr>
        <td align="center" width=100px>运算符</td>
        <td align="center" colspan="2" width=200px>说明</td>
        <td align="center">实例(设变量a=6, b=3)</td>
    </tr>
    <tr>
        <td align="center" width=100px>!</td>
        <td align="center" width=100px>非</td>
        <td align="center" width=100px>!</td>
        <td align="left">[ ! false ] 返回 true</td>
    </tr>
    <tr>
        <td align="center" width=100px>-o</td>
        <td align="center" width=100px>或</td>
        <td align="center" width=100px>|</td>
        <td align="left">[ $a -lt 7 -o $b -gt 10 ] 返回 true</td>
    </tr>
    <tr>
        <td align="center" width=100px>-a</td>
        <td align="center" width=100px>与</td>
        <td align="center" width=100px>&</td>
        <td align="left">[ $a -lt 7 -o $b -gt 10 ] 返回 false</td>
    </tr>
</table>



<h2 id="4-逻辑运算符"><a href="#4-逻辑运算符" class="headerlink" title="4.逻辑运算符"></a><font size=3>4.逻辑运算符</font></h2><table>
    <tr>
        <td align="center" width=100px>运算符</td>
        <td align="center" width=150px>说明</td>
        <td align="center">实例(设变量a=6, b=3)</td>
    </tr>
    <tr>
        <td align="center" width=100px>&&</td>
        <td align="center" width=150px>逻辑的 and </td>
        <td align="left">[[ $a -lt 10 && $b -gt 10 ]] 返回 false</td>
    </tr>
    <tr>
        <td align="center" width=100px>||</td>
        <td align="center" width=150px>逻辑的 or </td>
        <td align="left">[[ $a -lt 10 || $b -gt 10 ]] 返回 true</td>
    </tr>
</table>


<h2 id="5-字符串运算符"><a href="#5-字符串运算符" class="headerlink" title="5.字符串运算符"></a><font size=3>5.字符串运算符</font></h2><table>
    <tr>
        <td align="center" width=100px>运算符</td>
        <td align="center">说明</td>
        <td align="center">实例(设变量a="abc", b="abd")</td>
    </tr>
    <tr>
        <td align="center" width=100px>=</td>
        <td align="left">检测两个字符串是否相等，相等返回 true</td>
        <td align="left">[ $a = $b ] 返回 false</td>
    </tr>
    <tr>
        <td align="center" width=100px>!=</td>
        <td align="left">两个字符串是否不相等，不相等返回 true</td>
        <td align="left">[ $a != $b ] 返回 true</td>
    </tr>
    <tr>
        <td align="center" width=100px>-z</td>
        <td align="left">字符串长度是否为0，为0返回 true</td>
        <td align="left">[ -z $a ] 返回 false</td>
    </tr>
    <tr>
        <td align="center" width=100px>-n</td>
        <td align="left" >字符串长度是否不为 0，不为 0 返回 true</td>
        <td align="left">[ -n $a ] 返回 true</td>
    </tr>
    <tr>
        <td align="center" width=100px>$</td>
        <td align="left">字符串是否为空，不为空返回 true</td>
        <td align="left">[ $a ] 返回 true</td>
    </tr>
</table>



<h2 id="6-文件测试运算符"><a href="#6-文件测试运算符" class="headerlink" title="6.文件测试运算符"></a><font size=3>6.文件测试运算符</font></h2><p>文件测试运算符用于检测  Unix  文件的各种属性</p>
<table>
    <tr>
        <td align="center" width=120px>运算符</td>
        <td align="center">说明</td>
    </tr>
    <tr>
        <td align="center" width=120px>-b file</td>
        <td align="left">检测文件是否是块设备文件，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-c file</td>
        <td align="left">检测文件是否是字符设备文件，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-d file</td>
        <td align="left">检测文件是否是目录，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-f file</td>
        <td align="left">检测文件是否是普通文件（既不是目录，也不是设备文件），如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-g file</td>
        <td align="left">检测文件是否设置了 SGID 位，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-k file</td>
        <td align="left">检测文件是否设置了粘着位(Sticky Bit)，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-p file</td>
        <td align="left">检测文件是否是有名管道，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-u file</td>
        <td align="left">检测文件是否设置了 SUID 位，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-r file</td>
        <td align="left">检测文件是否可读，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-w file</td>
        <td align="left">检测文件是否可写，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-x file</td>
        <td align="left">检测文件是否可执行，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-s file</td>
        <td align="left">检测文件是否为空（文件大小是否大于0），不为空返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-e file</td>
        <td align="left">检测文件（包括目录）是否存在，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>-L file</td>
        <td align="left">检测测文件是否为符号链接，如果是，则返回 true。</td>
    </tr>
    <tr>
        <td align="center" width=120px>file1 -nt file2</td>
        <td align="left">测试文件 file1 是否比文件 file2 更新。</td>
    </tr>
    <tr>
        <td align="center" width=120px>file1 -ot file2</td>
        <td align="left">测试文件 file1 是否比文件 file2 更旧。</td>
    </tr>
</table>

<h1 id="二、说明性语句"><a href="#二、说明性语句" class="headerlink" title="二、说明性语句"></a><font size=3>二、说明性语句</font></h1><p>其实就是注释，以 # 号开始到该行结束，不被解释执行。 sh 里没有多行注释，只能每一行加一个#号。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">--------------------------------------------</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">这是一个注释</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">author：</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">file name：</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">description:</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">--------------------------------------------</span></span><br></pre></td></tr></table></figure>

<h1 id="三、功能性语句"><a href="#三、功能性语句" class="headerlink" title="三、功能性语句"></a><font size=3>三、功能性语句</font></h1><h2 id="1-readonly"><a href="#1-readonly" class="headerlink" title="1. readonly "></a><font size=3>1. readonly </font></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">readonly Variable # Variable为变量名称</span><br></pre></td></tr></table></figure>

<p>使用  readonly  命令可以<strong>将变量定义为只读变量</strong>，只读变量的值不能被改变。</p>
<h2 id="2-unset"><a href="#2-unset" class="headerlink" title="2. unset "></a><font size=3>2. unset </font></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unset Variable # Variable为变量名称</span><br></pre></td></tr></table></figure>

<p>该命令可以删除已经定义的变量。</p>
<h2 id="3-read"><a href="#3-read" class="headerlink" title="3. read "></a><font size=3>3. read </font></h2><h3 id="3-1使用格式"><a href="#3-1使用格式" class="headerlink" title="3.1使用格式"></a><font size=3>3.1使用格式</font></h3><p> read 用来从标准输入中读取数据并赋值给变量。如果没有进行重定向，默认就是从键盘读取用户输入的数据；如果进行了重定向，那么可以从文件中读取数据。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">read [option] [Variables]</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=80px rowspan="10">[option]</td>
        <td align="left" width=120px>-a array</td>
        <td align="left">把读取的数据赋值给数组 array，从下标 0 开始</td>
    </tr>
    <tr>
        <td align="left" width=120px>-d delimiter</td>
        <td align="left">用字符串 delimiter 指定读取结束的位置，而不是一个换行符（默认结束符号为换行符，读取到的数据不包括 delimiter）。</td>
    </tr>
    <tr>
        <td align="left" width=120px>-n num</td>
        <td align="left">读取 num 个字符就结束，而不是整行字符。如果没有读满 num 个字符就按下回车或遇到换行符，则也会结束读取。</td>
    </tr>
    <tr>
        <td align="left" width=120px>-N num</td>
        <td align="left">严格要求读满 num 个字符才自动结束读取，即使中途按下了回车或遇到了换行符也不结束，其中换行符或回车算一个字符。</td>
    </tr>
    <tr>
        <td align="left" width=120px>-p prompt</td>
        <td align="left">显示提示信息，提示内容为 prompt。默认不支持"\n"换行，要换行需要特殊处理(例如：通过 $'string \n' 就可以实现换行输入)</td>
    </tr>
    <tr>
        <td align="left" width=120px>-t timeout</td>
        <td align="left">设置超时时间，单位为秒。如果用户没有在指定时间内输入完成，那么 read 将会返回一个非 0 的退出状态，表示读取失败，即使已经输入了一部分。</td>
    </tr>
    <tr>
        <td align="left" width=120px>-u fd</td>
        <td align="left">使用文件描述符 fd 作为输入源，而不是标准输入，类似于重定向。</td>
    </tr>
    <tr>
        <td align="left" width=120px>-e</td>
        <td align="left">在输入的时候可以使用命令补全功能(Tab键)。</td>
    </tr>
    <tr>
        <td align="left" width=80px>-r</td>
        <td align="left">原样读取（Raw mode），不把反斜杠字符解释为转义字符，这意味着"\"会变成文本的一部分。</td>
    </tr>
    <tr>
        <td align="left" width=120px>-s</td>
        <td align="left">静默模式（Silent mode），不会在屏幕上显示输入的字符。可用于输入密码和其它确认信息的时候。</td>
    </tr>
    <tr>
        <td align="left" width=120px>[Variables]</td>
        <td align="left" colspan="2">变量名称</td>
    </tr>
</table>



<p><strong>【注意】</strong></p>
<p>（1）可以使用多个参数。</p>
<p>（2）变量读取说明</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">read [-ers] [-a aname] [-d delim] [-i text] [-n num] [-N num] [-p prompt] [-t timeout] [-u fd] [var_name1 var_name2 ...]</span><br></pre></td></tr></table></figure>

<p>首先说一下 IFS(internal field separator ，为<strong>内部字段分隔符</strong>，是 bash shell 中的环境变量，它定义了 bash shell 用作字段分隔符的一系列字符，默认情况下， bash shell 会将下列字符当做字段分隔符：<strong>空格</strong>、<strong>制表符</strong>和<strong>换行符</strong>。</p>
<p> read 命令用于从标准输入中读取输入单行，并将读取的单行根据 IFS 环境变量分裂成多个字段，并将分割后的字段分别赋值给指定的变量列表 var_name 。第一个字段分配给第一个变量 var_name1 ，第二个字段分配给第二个变量 var_name2 ，依次到结束。如果指定的变量名少于字段数量，则多出的字段数量也同样分配给最后一个 var_name ，如果指定的变量命令多于字段数量，则多出的变量赋值为空。如果没有指定任何 var_name ，则分割后的所有字段都存储在环境变量 REPLY 中。</p>
<h3 id="3-2使用实例"><a href="#3-2使用实例" class="headerlink" title="3.2使用实例"></a><font size=3>3.2使用实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">read -p $&#x27;Enter your name: \n&#x27;</span><br><span class="line">echo $REPLY</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Enter your name: </span><br><span class="line">qidaink</span><br><span class="line">qidaink</span><br></pre></td></tr></table></figure>

<p>（3）给多个变量赋值时，必须在一行内输入所有的值，不能换行，否则只能给第一个变量赋值，后续变量都会赋值失败。</p>
<h2 id="4-test"><a href="#4-test" class="headerlink" title="4. test "></a><font size=3>4. test </font></h2><p> test  命令用于<strong>检查某个条件是否成立</strong>，它可以进行数值、字符和文件三个方面的测试。通常和  if  语句一起使用，并且大部分  if  语句都依赖  test 。</p>
<p>以下只为举例说明使用格式，还有一些运算符可以查看本篇笔记的《运算符》。</p>
<h3 id="4-1数值测试"><a href="#4-1数值测试" class="headerlink" title="4.1数值测试"></a><font size=3>4.1数值测试</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">test $[num1] &lt;关系运算符&gt; $[num2]</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px rowspan="6">运算符</td>
        <td align="center">-eq</td>
        <td align="left">等于，则为真</td>
    </tr>
    <tr>
        <td align="center">-ne</td>
        <td align="left">不等于，则为真</td>
    </tr>
    <tr>
        <td align="center">-gt</td>
        <td align="left">大于，则为真</td>
    </tr>
    <tr>
        <td align="center">-ge</td>
        <td align="left">大于等于，则为真</td>
    </tr>
    <tr>
        <td align="center">-lt</td>
        <td align="left">小于，则为真</td>
    </tr>
    <tr>
        <td align="center">-le</td>
        <td align="left">小于等于，则为真</td>
    </tr>
</table>

<h3 id="4-2字符串测试"><a href="#4-2字符串测试" class="headerlink" title="4.2字符串测试"></a><font size=3>4.2字符串测试</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">test $[num1] &lt;字符串运算符&gt; $[num2]</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px rowspan="6">运算符</td>
        <td align="center">=</td>
        <td align="left">等于，则为真</td>
    </tr>
    <tr>
        <td align="center">！=</td>
        <td align="left">不等于，则为真</td>
    </tr>
    <tr>
        <td align="center">-z 字符串</td>
        <td align="left">字符串的长度为零，则为真</td>
    </tr>
    <tr>
        <td align="center">-n 字符串</td>
        <td align="left">字符串的长度不为零，则为真</td>
    </tr>
</table>

<h3 id="4-3文件测试"><a href="#4-3文件测试" class="headerlink" title="4.3文件测试"></a><font size=3>4.3文件测试</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">test &lt;文件测试运算符&gt; file_name</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px rowspan="6">运算符</td>
        <td align="center">-e file</td>
        <td align="left">如果文件存在，则为真</td>
    </tr>
    <tr>
        <td align="center">-f file</td>
        <td align="left">如果文件存在且可读，则为真</td>
    </tr>
</table>

<h2 id="5-echo"><a href="#5-echo" class="headerlink" title="5. echo "></a><font size=3>5. echo </font></h2><h3 id="5-1使用格式"><a href="#5-1使用格式" class="headerlink" title="5.1使用格式"></a><font size=3>5.1使用格式</font></h3><p> echo 用于在屏幕上打印出指定的字符串，命令格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo [option] string</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px>-n</td>
        <td align="left" colspan="2">禁止在最后自动换行</td>
    </tr>
    <tr>
        <td align="center" width=100px rowspan="11">-e</td>
        <td align="left" colspan="2">开启转义字符功能，即遇到以下字符进行处理，而不会将其当成一般字符输出</td>
    </tr>
    <tr>
        <td align="center">\a</td>
        <td align="left">警报，响铃</td>
    </tr>
    <tr>
        <td align="center">\b</td>
        <td align="left">退格（删除键）</td>
    </tr>
    <tr>
        <td align="center">\f</td>
        <td align="left">换页(FF)，将当前位置移到下页开头</td>
    </tr>
    <tr>
        <td align="center">\n</td>
        <td align="left">换行</td>
    </tr>
    <tr>
        <td align="center">\c</td>
        <td align="left">不换行</td>
    </tr>
    <tr>
        <td align="center">\r</td>
        <td align="left">回车</td>
    </tr>
    <tr>
        <td align="center">\t</td>
        <td align="left">水平制表符（tab键）</td>
    </tr>
    <tr>
        <td align="center">\v</td>
        <td align="left">垂直制表符</td>
    </tr>
    <tr>
        <td align="center">\ooo</td>
        <td align="left">插入ooo（八进制）所代表的ASCII字符</td>
    </tr>
    <tr>
        <td align="center">\\</td>
        <td align="left">反斜杠</td>
    </tr>
    <tr>
        <td align="center">--version</td>
        <td align="left" colspan="2">显示版本信息</td>
    </tr>
    <tr>
        <td align="center">--helo</td>
        <td align="left" colspan="2">显示帮助</td>
    </tr>
</table>

<p><strong>【注意】</strong>该命令不带参数时<strong>自带换行</strong>。</p>
<h3 id="5-2使用实例"><a href="#5-2使用实例" class="headerlink" title="5.2使用实例"></a><font size=3>5.2使用实例</font></h3><h4 id="5-2-1显示普通字符串"><a href="#5-2-1显示普通字符串" class="headerlink" title="5.2.1显示普通字符串"></a><font size=3>5.2.1显示普通字符串</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">echo &quot;string1&quot;</span><br><span class="line">echo &quot;string2&quot;</span><br><span class="line">echo string3</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">string1</span><br><span class="line">string2</span><br><span class="line">string3</span><br></pre></td></tr></table></figure>

<h4 id="5-2-2显示转义字符"><a href="#5-2-2显示转义字符" class="headerlink" title="5.2.2显示转义字符"></a><font size=3>5.2.2显示转义字符</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">echo &quot;\&quot;string\&quot;&quot;</span><br><span class="line">echo \&quot;string\&quot;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&quot;string&quot;</span><br><span class="line">&quot;string&quot;</span><br></pre></td></tr></table></figure>

<h4 id="5-2-3显示变量"><a href="#5-2-3显示变量" class="headerlink" title="5.2.3显示变量"></a><font size=3>5.2.3显示变量</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">name=qidaink</span><br><span class="line">echo $name</span><br><span class="line">echo &quot;$name&quot;</span><br><span class="line">echo &quot;$&#123;name&#125;&quot;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">qidaink</span><br><span class="line">qidaink</span><br><span class="line">qidaink</span><br></pre></td></tr></table></figure>

<h4 id="5-2-4显示换行与不换行"><a href="#5-2-4显示换行与不换行" class="headerlink" title="5.2.4显示换行与不换行"></a><font size=3>5.2.4显示换行与不换行</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">name=qidaink</span><br><span class="line">echo -e &quot;$name\nString1&quot; # 开启转义字符，\n 换行</span><br><span class="line">echo  &quot;$name\nString1&quot;</span><br><span class="line"></span><br><span class="line">echo -e &quot;$name\c&quot;        # 开启转义字符，\c不换行</span><br><span class="line">echo &quot;String2&quot;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">qidaink</span><br><span class="line">String1</span><br><span class="line">qidaink\nString1</span><br><span class="line">qidainkString2</span><br></pre></td></tr></table></figure>

<h4 id="5-2-5显示结果定向至文件"><a href="#5-2-5显示结果定向至文件" class="headerlink" title="5.2.5显示结果定向至文件"></a><font size=3>5.2.5显示结果定向至文件</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">name=qidaink</span><br><span class="line">echo -e &quot;$name\nString1&quot; &gt; file.txt</span><br></pre></td></tr></table></figure>

<p>运行结果：会在当前 Shell 脚本所在目录创建一个 filt.txt 文件（无该文件的话），并将要显示的内容显示在 filt.txt 文件中。</p>
<h4 id="5-2-6原样输出字符串"><a href="#5-2-6原样输出字符串" class="headerlink" title="5.2.6原样输出字符串"></a><font size=3>5.2.6原样输出字符串</font></h4><ul>
<li>原样输出字符串，不进行转义或取变量(用单引号)</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">name=qidaink</span><br><span class="line">echo &quot;$name\nString1&quot;</span><br><span class="line">echo &#x27;$name\nString1&#x27;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">qidaink\nString1</span><br><span class="line"><span class="meta prompt_">$</span><span class="language-bash">name\nString1</span></span><br></pre></td></tr></table></figure>

<h4 id="5-2-7显示命令执行结果"><a href="#5-2-7显示命令执行结果" class="headerlink" title="5.2.7显示命令执行结果"></a><font size=3>5.2.7显示命令执行结果</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">echo  date </span><br><span class="line">echo &#x27;date&#x27;</span><br><span class="line">echo &quot;date&quot;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">2022年 02月 14日 星期一 13:29:17 CST</span><br><span class="line">date</span><br><span class="line">date</span><br></pre></td></tr></table></figure>

<h2 id="6-printf"><a href="#6-printf" class="headerlink" title="6. printf "></a><font size=3>6. printf </font></h2><h3 id="6-1使用格式"><a href="#6-1使用格式" class="headerlink" title="6.1使用格式"></a><font size=3>6.1使用格式</font></h3><p>printf  由  POSIX  标准所定义，因此使用  printf  的脚本比使用  echo  移植性好。 printf  使用<strong>引用文本或空格分隔</strong>的参数，外面可以在  printf 中使用格式化字符串，还可以制定字符串的宽度、左右对齐方式等。默认的  printf  不会像 echo  自动添加换行符。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">printf  &quot;format-string&gt;&quot;  [arguments...]</span><br></pre></td></tr></table></figure>

<p>其中 format-string  为格式控制字符串， arguments  为参数列表。 Shell 脚本中的 printf 和 C 语言中的 printf 用法基本一致，只是在写法上有些许区别。</p>
<ul>
<li>printf  命令不用加括号</li>
<li>format-string  可以没有引号，但最好加上，单引号双引号均可。</li>
<li>参数多于格式控制符( % )时， format-string  可以重用，可以将所有参数都转换。</li>
<li>arguments  使用<strong>空格分隔</strong>，不用逗号。</li>
</ul>
<h3 id="6-2-转义字符"><a href="#6-2-转义字符" class="headerlink" title="6.2 转义字符"></a><font size=3>6.2 转义字符</font></h3><table>
    <tr>
        <td align="center" width=150px>\a</td>
        <td align="left">警报，响铃</td>
    </tr>
    <tr>
        <td align="center" width=150px>\b</td>
        <td align="left">退格（删除键）</td>
    </tr>
    <tr>
        <td align="center" width=150px>\c</td>
        <td align="left">不显示输出结果中任何结尾的换行字符，而且任何留在参数里的字符、任何接下来的参数以及任何留在格式字符串中的字符都被忽略。</td>
    </tr>
    <tr>
        <td align="center" width=150px>\f</td>
        <td align="left">换页(FF)，将当前位置移到下页开头</td>
    </tr>
    <tr>
        <td align="center" width=150px>\n</td>
        <td align="left">换行</td>
    </tr>
    <tr>
        <td align="center" width=150px>\r</td>
        <td align="left">回车</td>
    </tr>
    <tr>
        <td align="center" width=150px>\t</td>
        <td align="left">水平制表符（tab键）</td>
    </tr>
    <tr>
        <td align="center" width=150px>\v</td>
        <td align="left">垂直制表符</td>
    </tr>
    <tr>
        <td align="center" width=150px>\ooo</td>
        <td align="left">插入ooo（八进制）所代表的ASCII字符</td>
    </tr>
    <tr>
        <td align="center" width=150px>\\</td>
        <td align="left">反斜杠字符</td>
    </tr>
</table>

<h3 id="6-3-格式控制字符串"><a href="#6-3-格式控制字符串" class="headerlink" title="6.3 格式控制字符串"></a><font size=3>6.3 格式控制字符串</font></h3><h4 id="6-3-1格式控制字符"><a href="#6-3-1格式控制字符" class="headerlink" title="6.3.1格式控制字符"></a><font size=3>6.3.1格式控制字符</font></h4><table>
    <tr>
        <td align="center" width=100px>格式字符</td>
        <td align="center">形式</td>
        <td align="center">意义</td>
    </tr>
    <tr>
        <td align="center" width=100px>i, d</td>
        <td align="center">%i, %d</td>
        <td align="left">以十进制形式输出带符号整数(正数不输出符号)</td>
    </tr>
    <tr>
        <td align="center" width=100px>u</td>
        <td align="center">%u</td>
        <td align="left">以十进制形式输出无符号整数</td>
    </tr>
    <tr>
        <td align="center" width=100px>o</td>
        <td align="center">%o</td>
        <td align="left">以八进制形式输出无符号整数(不输出前缀0)</td>
    </tr>
    <tr>
        <td align="center" width=100px>x, X</td>
        <td align="center">%x, %X</td>
        <td align="left">以十六进制形式输出无符号整数(不输出前缀0x)</td>
    </tr>
    <tr>
        <td align="center" width=100px>f</td>
        <td align="center">%f</td>
        <td align="left">以小数形式输出单、双精度实数</td>
    </tr>
    <tr>
        <td align="center" width=100px>e, E</td>
        <td align="center">%e, %E</td>
        <td align="left">以指数形式输出单、双精度实数</td>
    </tr>
    <tr>
        <td align="center" width=100px>g, G</td>
        <td align="center">%g, %G</td>
        <td align="left">以 %f 或 %e 中较短的输出宽度输出单、双精度实数</td>
    </tr>
    <tr>
        <td align="center" width=100px>c</td>
        <td align="center">%c</td>
        <td align="left">输出单个字符</td>
    </tr>
    <tr>
        <td align="center" width=100px>s</td>
        <td align="center">%s</td>
        <td align="left">输出字符串</td>
    </tr>
</table>



<h4 id="6-3-2标识"><a href="#6-3-2标识" class="headerlink" title="6.3.2标识"></a><font size=3>6.3.2标识</font></h4><table>
    <tr>
        <td align="center" width=100px>标识</td>
        <td align="center">描述</td>
    </tr>
    <tr>
        <td align="center" width=100px>-</td>
        <td align="left">在给定的字段宽度内左对齐，默认是右对齐</td>
    </tr>
    <tr>
        <td align="center" width=100px>+</td>
        <td align="left">强制在结果之前显示加号或减号（+ 或 -），即正数前面会显示 + 号。默认情况下，只有负数前面会显示一个 - 号。</td>
    </tr>
    <tr>
        <td align="center" width=100px>space<br>(空格)</td>
        <td align="left">有符号值若为正，则在值前显示前导空格（但是不显示符号）；若为负，则在值前显示-。</td>
    </tr>
    <tr>
        <td align="center" width=100px rowspan="3">#</td>
        <td align="left">与 o、x 或 X 说明符一起使用时，非零值前面会分别显示 0、0x 或 0X。</td>
    </tr>
    <tr>
        <td align="left">与 e、E 和 f 一起使用时，会强制输出包含一个小数点，即使后边没有数字时也会显示小数点。默认情况下，如果后边没有数字时候，不会显示显示小数点。</td>
    </tr>
    <tr>
        <td align="left">与 g 或 G 一起使用时，结果与使用 e 或 E 时相同，但是尾部的零不会被移除。</td>
    </tr>
    <tr>
        <td align="center" width=100px>0</td>
        <td align="left">输出数值时指定左面不使用的空位置自动填0。
</td>
    </tr>
</table>



<h4 id="6-3-3最小输出宽度（-width-）"><a href="#6-3-3最小输出宽度（-width-）" class="headerlink" title="6.3.3最小输出宽度（ width ）"></a><font size=3>6.3.3最小输出宽度（ width ）</font></h4><table>
    <tr>
        <td align="center" width=100px>宽度</td>
        <td align="center">描述</td>
    </tr>
    <tr>
        <td align="center" width=100px>number</td>
        <td align="left">数值（十进制整数），数据长度 小于number，则左补空格；否则按实际输出。</td>
    </tr>
    <tr>
        <td align="center" width=100px>*</td>
        <td align="left">星号，精度在 format 字符串中未指定，但是会作为附加整数值参数放置于要被格式化的参数之前。</td>
    </tr>
</table>



<h4 id="6-3-4精度（-precision-）"><a href="#6-3-4精度（-precision-）" class="headerlink" title="6.3.4精度（ .precision ）"></a><font size=3>6.3.4精度（ .precision ）</font></h4><table>
    <tr>
        <td align="center" width=100px>精度</td>
        <td align="center">描述</td>
    </tr>
    <tr><!-- 1 -->
        <td align="center" width=100px rowspan="6">.number</td>
        <td align="left">对于整数说明符（d、i、o、u、x、X）：precision 指定了要写入的数字的最小位数。如果写入的值短于该数，结果会用前导零来填充。如果写入的值长于该数，结果不会被截断。精度为 0 意味着不写入任何字符。</td>
    </tr>
    <tr><!-- 2 -->
        <td align="left">对于 e、E 和 f 说明符：要在小数点后输出的小数位数。</td>
    </tr>
    <tr><!-- 3 -->
        <td align="left">对于 g 和 G 说明符：要输出的最大有效位数。</td>
    </tr>
    <tr><!-- 4 -->
        <td align="left">对于 s : 要输出的最大字符数,不足number则正常输出，超过则截断。默认情况下，所有字符都会被输出，直到遇到末尾的空字符。</td>
    </tr>
    <tr><!-- 5 -->
        <td align="left">对于 c 类型：没有任何影响。</td>
    </tr>
    <tr><!-- 6 -->
        <td align="left">当未指定任何精度时，默认为 1。如果指定时不带有一个显式值，则假定为 0。</td>
    </tr>
    <tr>
        <td align="center" width=100px>.*</td>
        <td align="left">星号，精度在 format 字符串中未指定，但是会作为附加整数值参数放置于要被格式化的参数之前。<br>但是在尝试的时候似乎有一些数据类型不匹配的情况，不过也用的不多。</td>
    </tr>
</table>



<h3 id="6-4使用实例"><a href="#6-4使用实例" class="headerlink" title="6.4使用实例"></a><font size=3>6.4使用实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">printf &quot;%-10s %-8s %-4s\n&quot; name sex kg  </span><br><span class="line">printf &quot;%-10s %-8s %-4.1f\n&quot; qidai 男 72.162</span><br><span class="line">printf &quot;%-10s %-8s %-4.1f\n&quot; fanhua 男 60.9612</span><br><span class="line">printf &quot;%-10s %-8s %-4.1f\n&quot; yunyuhai 女 48.96313141</span><br></pre></td></tr></table></figure>

<h1 id="四、结构性语句"><a href="#四、结构性语句" class="headerlink" title="四、结构性语句"></a><font size=3>四、结构性语句</font></h1><p>结构性语句主要根据程序的运行状态、输入数据、变量的取值、控制信号以及运行时间等因素来控制程序的运行流程。主要包括：条件测试语句（两路分支）、多路分支语句、循环语句、循环控制语句和后台执行语句等。</p>
<h2 id="1-分支语句"><a href="#1-分支语句" class="headerlink" title="1.分支语句"></a><font size=3>1.分支语句</font></h2><h3 id="1-1-if…else…-语句"><a href="#1-1-if…else…-语句" class="headerlink" title="1.1 if…else… 语句"></a><font size=3>1.1 if…else… 语句</font></h3><h4 id="1-1-1使用格式1"><a href="#1-1-1使用格式1" class="headerlink" title="1.1.1使用格式1"></a><font size=3>1.1.1使用格式1</font></h4><ul>
<li>语法结构 1</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="keyword">if</span> ... <span class="keyword">fi</span> 结构</span></span><br><span class="line">if [ expression ]</span><br><span class="line">then</span><br><span class="line">   Statement(s) to be executed if expression is true</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">if [ expression ] ; then</span><br><span class="line">   Statement(s) to be executed if expression is true</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者写成一行</span></span><br><span class="line">if [ expression ] ; then Statement(s) to be executed if expression is true; fi;</span><br></pre></td></tr></table></figure>

<h4 id="1-1-2使用格式2"><a href="#1-1-2使用格式2" class="headerlink" title="1.1.2使用格式2"></a><font size=3>1.1.2使用格式2</font></h4><ul>
<li>语法结构 2</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="keyword">if</span> ... <span class="keyword">else</span> ... <span class="keyword">fi</span> 结构</span></span><br><span class="line">if [ expression ]</span><br><span class="line">then</span><br><span class="line">   Statement(s) to be executed if expression is true</span><br><span class="line">else</span><br><span class="line">   Statement(s) to be executed if expression is not true</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>

<h4 id="1-1-3使用格式3"><a href="#1-1-3使用格式3" class="headerlink" title="1.1.3使用格式3"></a><font size=3>1.1.3使用格式3</font></h4><ul>
<li>语法结构 3</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="keyword">if</span> ... <span class="keyword">elif</span> ... <span class="keyword">fi</span> 结构</span></span><br><span class="line">if [ expression 1 ]</span><br><span class="line">then</span><br><span class="line">   Statement(s) to be executed if expression 1 is true</span><br><span class="line">elif [ expression 2 ]</span><br><span class="line">then</span><br><span class="line">   Statement(s) to be executed if expression 2 is true</span><br><span class="line">elif [ expression 3 ]</span><br><span class="line">then</span><br><span class="line">   Statement(s) to be executed if expression 3 is true</span><br><span class="line">else</span><br><span class="line">   Statement(s) to be executed if no expression is true</span><br><span class="line">f</span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）以上三种结构中， if 和 elif 之后要跟一个 then ，结束要有 fi ，中间的执行语句不需要像 C语言 一样有 {} 。</p>
<p>（2） then 可以与 if 或者 elif 写在同一行，但是要加一个 ; </p>
<p>（3） if 也常与 test 命令一起使用。</p>
<h3 id="1-2-case-语句"><a href="#1-2-case-语句" class="headerlink" title="1.2 case 语句"></a><font size=3>1.2 case 语句</font></h3><h4 id="1-2-1使用格式"><a href="#1-2-1使用格式" class="headerlink" title="1.2.1使用格式"></a><font size=3>1.2.1使用格式</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">case 值 in</span><br><span class="line">    模式1)</span><br><span class="line">        command1</span><br><span class="line">        command2</span><br><span class="line">        command3</span><br><span class="line">        ;;</span><br><span class="line">    模式2）</span><br><span class="line">        command1</span><br><span class="line">        command2</span><br><span class="line">        command3</span><br><span class="line">        ;;</span><br><span class="line">    *)</span><br><span class="line">        command1</span><br><span class="line">        command2</span><br><span class="line">        command3</span><br><span class="line">        ;;</span><br><span class="line">esac</span><br></pre></td></tr></table></figure>

<p><strong>【注意】</strong></p>
<p>（1）取值后面必须为单词  in ，<strong>每一模式必须以右括号结束</strong>。取值可以为变量或常数，匹配发现取值符合某一模式后，其间所有命令开始执行直至  ;; 。</p>
<p>（2）每个  case  分支用<strong>右圆括号</strong>开始，用两个分号  ;; 表示  break ，即执行结束。如果没有匹配到任何一个模式，使用星号  *  捕获该值，再执行后面的命令。</p>
<p>（3） esac （就是  case  反过来）作为整个结构结束标记。</p>
<h4 id="1-2-2使用实例"><a href="#1-2-2使用实例" class="headerlink" title="1.2.2使用实例"></a><font size=3>1.2.2使用实例</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">read -p &quot;Inter a number:&quot; value</span><br><span class="line">if [ $value -lt 0 ] || [ $value -gt 100 ]</span><br><span class="line">    then</span><br><span class="line">    echo &quot;input error.&quot;</span><br><span class="line">    exit 0</span><br><span class="line">else</span><br><span class="line">    value= expr $value / 10 </span><br><span class="line">    case $value in </span><br><span class="line">        8 | 9 | 10)</span><br><span class="line">            echo  &quot;A&quot;</span><br><span class="line">            ;;</span><br><span class="line">        6 | 7)</span><br><span class="line">            echo  &quot;B&quot;</span><br><span class="line">            ;;</span><br><span class="line">        *)</span><br><span class="line">            echo  &quot;C&quot;</span><br><span class="line">            ;;</span><br><span class="line">    esac</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>

<h2 id="2-循环语句"><a href="#2-循环语句" class="headerlink" title="2.循环语句"></a><font size=3>2.循环语句</font></h2><h3 id="2-1-for-循环"><a href="#2-1-for-循环" class="headerlink" title="2.1 for 循环"></a><font size=3>2.1 for 循环</font></h3><h4 id="2-1-1使用格式1"><a href="#2-1-1使用格式1" class="headerlink" title="2.1.1使用格式1"></a><font size=3>2.1.1使用格式1</font></h4><ul>
<li>语法结构 1</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">for 变量 in 列表</span><br><span class="line">do</span><br><span class="line">    command1</span><br><span class="line">    command2</span><br><span class="line">    ...</span><br><span class="line">    commandN</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h4 id="2-1-2使用格式2"><a href="#2-1-2使用格式2" class="headerlink" title="2.1.2使用格式2"></a><font size=3>2.1.2使用格式2</font></h4><ul>
<li>语法结构 2</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">for((表达式1; 表达式2; 表达式2))</span><br><span class="line">do</span><br><span class="line">    command1</span><br><span class="line">    command2</span><br><span class="line">    ...</span><br><span class="line">    commandN</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）以上两种结构都可以，但是都要注意不可省略 do 和 done 。</p>
<h4 id="2-1-3使用实例"><a href="#2-1-3使用实例" class="headerlink" title="2.1.3使用实例"></a><font size=3>2.1.3使用实例</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">printf &quot;\033[31m shell方式 \033[0m\n&quot;</span><br><span class="line">for value in 1 2 3 4 5</span><br><span class="line">    do</span><br><span class="line">        printf &quot;$value&quot;</span><br><span class="line">    done</span><br><span class="line">    echo &quot;&quot;</span><br><span class="line"></span><br><span class="line">printf &quot;\033[31m 类似C语言方式 \033[0m\n&quot;</span><br><span class="line">for ((val = 1; val &lt;=5; val++))</span><br><span class="line">    do</span><br><span class="line">        printf &quot;$val&quot;</span><br><span class="line">    done</span><br><span class="line">    echo &quot;&quot;</span><br></pre></td></tr></table></figure>

<h3 id="2-2-while-循环"><a href="#2-2-while-循环" class="headerlink" title="2.2 while 循环"></a><font size=3>2.2 while 循环</font></h3><h4 id="2-2-1使用格式1"><a href="#2-2-1使用格式1" class="headerlink" title="2.2.1使用格式1"></a><font size=3>2.2.1使用格式1</font></h4><ul>
<li>语法结构 1</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">while [ expression ]</span><br><span class="line">do</span><br><span class="line">   Statement(s) to be executed if expression is true</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h4 id="2-2-2使用格式2"><a href="#2-2-2使用格式2" class="headerlink" title="2.2.2使用格式2"></a><font size=3>2.2.2使用格式2</font></h4><ul>
<li>语法结构 2</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">while (( expression )) </span><br><span class="line">do</span><br><span class="line">   Statement(s) to be executed if expression is true</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h4 id="2-2-3使用实例"><a href="#2-2-3使用实例" class="headerlink" title="2.2.3使用实例"></a><font size=3>2.2.3使用实例</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">语法结构1例子</span></span><br><span class="line">i=1</span><br><span class="line">sum=0</span><br><span class="line">while [ $i -le 10 ]</span><br><span class="line">do</span><br><span class="line">	sum= expr $sum + $i </span><br><span class="line">	i= expr $i + 1 </span><br><span class="line">done</span><br><span class="line">echo &quot;i = $i  sum = $sum&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">语法结构2例子</span></span><br><span class="line">num=0</span><br><span class="line">while (( $num &lt; 5))</span><br><span class="line">do</span><br><span class="line">	echo &quot;$num&quot;</span><br><span class="line">	num= expr $num + 1 </span><br><span class="line">done</span><br><span class="line">echo &quot;num=$num&quot;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-until-循环"><a href="#2-3-until-循环" class="headerlink" title="2.3 until 循环"></a><font size=3>2.3 until 循环</font></h3><p> until  循环执行一系列命令直至条件为  true  时停止。 until  循环与  while  循环在处理方式上刚好相反。一般  while  循环优于  until  循环，但在某些时候—也只是极少数情况下， until  循环更加有用。</p>
<h4 id="2-3-1使用格式"><a href="#2-3-1使用格式" class="headerlink" title="2.3.1使用格式"></a><font size=3>2.3.1使用格式</font></h4><ul>
<li>语法结构</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">until [ expression ]</span><br><span class="line">do</span><br><span class="line">   Statement(s) to be executed until command is true</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h4 id="2-3-2使用实例"><a href="#2-3-2使用实例" class="headerlink" title="2.3.2使用实例"></a><font size=3>2.3.2使用实例</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">num=0</span><br><span class="line">until [ ! $&#123;num&#125; -lt 3 ]</span><br><span class="line">do</span><br><span class="line">   echo $&#123;num&#125;</span><br><span class="line">   num= expr $&#123;num&#125; + 1 </span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h3 id="2-4无限循环写法"><a href="#2-4无限循环写法" class="headerlink" title="2.4无限循环写法"></a><font size=3>2.4无限循环写法</font></h3><h4 id="2-4-1-for"><a href="#2-4-1-for" class="headerlink" title="2.4.1 for "></a><font size=3>2.4.1 for </font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="keyword">for</span>写法</span></span><br><span class="line">for (( ; ; ))</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h4 id="2-4-2-while"><a href="#2-4-2-while" class="headerlink" title="2.4.2 while "></a><font size=3>2.4.2 while </font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="keyword">while</span>写法1</span></span><br><span class="line">while :</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash"><span class="keyword">while</span>写法2</span></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h3 id="2-5跳出循环"><a href="#2-5跳出循环" class="headerlink" title="2.5跳出循环"></a><font size=3>2.5跳出循环</font></h3><h4 id="2-5-1使用格式"><a href="#2-5-1使用格式" class="headerlink" title="2.5.1使用格式"></a><font size=3>2.5.1使用格式</font></h4><p>在循环过程中，有时候需要在未达到循环结束条件时强制跳出循环， Shell 与 C 语言一致，使用两个命令来实现该功能： break 和 continue 。用法与 C 语言一致。</p>
<p><strong>【注意】</strong></p>
<p>（1）一般来说 break 是跳出本层循环， continue 是结束本次循环，直接进入下一次循环。</p>
<p>（2）在嵌套循环中， break  命令后面还可以跟一个整数，表示跳出第几层循环。 continue 后边也可以跟一个整数，表示继续第几层循环。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">break n      # break所在循环层为第1层，然后再向外计算层数</span><br><span class="line">continue n   # continue所在循环层为第1层，然后再向外计算层数</span><br></pre></td></tr></table></figure>

<h4 id="2-5-2使用实例"><a href="#2-5-2使用实例" class="headerlink" title="2.5.2使用实例"></a><font size=3>2.5.2使用实例</font></h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">for var1 in 1 2</span><br><span class="line">do</span><br><span class="line">   echo &quot;第1层循环 var1=$&#123;var1&#125;&quot;</span><br><span class="line">   for var2 in 3 4</span><br><span class="line">   do</span><br><span class="line">      echo -e &quot;\t第2层循环 var2=$&#123;var2&#125;&quot;</span><br><span class="line">      for var3 in 5 6</span><br><span class="line">      do</span><br><span class="line">         if [ $&#123;var1&#125; == 1 ] &amp;&amp; [ $&#123;var2&#125; == 4 ] &amp;&amp; [ $&#123;var3&#125; == 5 ]</span><br><span class="line">         then</span><br><span class="line">            echo -e &quot;break 跳出&quot;</span><br><span class="line">            break 3</span><br><span class="line">         elif [ $&#123;var1&#125; == 1 ] &amp;&amp; [ $&#123;var2&#125; == 3 ] &amp;&amp; [ $&#123;var3&#125; == 5 ]</span><br><span class="line">         then</span><br><span class="line">            echo -e &quot;continue 继续&quot;</span><br><span class="line">            continue 3</span><br><span class="line">         fi</span><br><span class="line">            echo -e &quot;\t\t第3层循环 var3=$&#123;var3&#125;&quot;</span><br><span class="line">            echo -e &quot;\t\t一次循环结束 $&#123;var1&#125; $&#123;var2&#125; $&#123;var3&#125;&quot;</span><br><span class="line">      done</span><br><span class="line">   done</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h2 id="3-三目运算符？"><a href="#3-三目运算符？" class="headerlink" title="3.三目运算符？"></a><font size=3>3.三目运算符？</font></h2><h3 id="3-1使用格式-1"><a href="#3-1使用格式-1" class="headerlink" title="3.1使用格式"></a><font size=3>3.1使用格式</font></h3><p>我也不知道叫啥，其实就很类似于条件语句：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">command1 &amp;&amp; command2 || command3</span><br><span class="line"></span><br><span class="line">command1 &amp;&amp; &#123; command2_1; command2_2; command2_3;&#125; || &#123; command3_1; command3_3; command3_3;&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="3-2-使用实例"><a href="#3-2-使用实例" class="headerlink" title="3.2 使用实例"></a><font size=3>3.2 使用实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">fileName 文件不存在，则退出，就可以按照下面方式执行</span></span><br><span class="line">[ -e $fileName ] || &#123; echo -e &quot;fileName Not existed!&quot;; exit 1; &#125;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">也或者可以增加一些 <span class="built_in">log</span> 打印信息</span></span><br><span class="line">[ -e $fileName ] &amp;&amp; echo -e &quot;$fileName existed&quot; || &#123; echo -e &quot;$fileName Not existed!&quot;; exit 1; &#125;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">多个命令集合的组合</span></span><br><span class="line">[ -e $fileName ] &amp;&amp; echo -e &quot;$fileName existed&quot;; ehco -e &quot;Other Necessary Information&quot; || &#123; echo -e &quot;$fileName Not existed!&quot;; exit 1; &#125;</span><br><span class="line">[ -e $fileName ] &amp;&amp; &#123; echo -e &quot;$fileName existed&quot;; ehco -e &quot;Other Necessary Information&quot;; &#125; || &#123; echo -e &quot;$fileName Not existed!&quot;; exit 1; &#125;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">读取IP地址，若为空，则使用默认IP，否则使用新的IP地址</span></span><br><span class="line">read -p &quot;Please input Management IP (Default is $DEFAULT_IP): &quot; MGMT_IP </span><br><span class="line">[[ -z $MGMT_IP ]] &amp;&amp; &#123; MGMT_IP=$DEFAULT_IP; echo -e &quot;Using default IP $MGMT_IP\n&quot; ;&#125; || DEFAULT_IP=$MGMT_IP</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h1 id="五、-Shell-中带颜色的输出"><a href="#五、-Shell-中带颜色的输出" class="headerlink" title="五、 Shell 中带颜色的输出"></a><font size=3>五、 Shell 中带颜色的输出</font></h1><h2 id="1-ANSI-控制码简介"><a href="#1-ANSI-控制码简介" class="headerlink" title="1. ANSI 控制码简介"></a><font size=3>1. ANSI 控制码简介</font></h2><p>ANSI 控制码用于在字符显示系统中控制光标移动和字符色彩等，常用于 BBS 系统中。 ANSI ESCAPE SEQUENCES 又称为 VT100 系列控制码，国内译为 ANSI 控制码。 ANSI 控制码依赖终端，不是依赖语言，所以在 shell ， perl ， C 里应用都是可以的。</p>
<p>ANSI 控制码开始的标志都为 ESC[ ， ESC 对应 ASCII码表 的 033(八进制) ，在 Shell 编程中，输出的命令有 echo 和 printf ， echo 需要用 -e 启用转义，两个输出命令通过 \033或者\e 来输入 ESC ，例如 \033[32m 即为 ESC[32m 。</p>
<h2 id="2-ANSI-控制码"><a href="#2-ANSI-控制码" class="headerlink" title="2. ANSI 控制码"></a><font size=3>2. ANSI 控制码</font></h2><table>
    <tr>
        <td align="center">\033[0m</td>
        <td align="left">关闭所有属性 </td>
    </tr>
    <tr>
        <td align="center">\033[1m</td>
        <td align="left">设置高亮度</td>
    </tr>
    <tr>
        <td align="center">\033[4m</td>
        <td align="left">下划线 </td>
    </tr>
    <tr>
        <td align="center">\033[5m</td>
        <td align="left">闪烁 </td>
    </tr>
    <tr>
        <td align="center">\033[7m</td>
        <td align="left">反显 </td>
    </tr>
    <tr>
        <td align="center">\033[8m</td>
        <td align="left">消隐 </td>
    </tr>
    <tr>
        <td align="center">\033[30m ~ \033[37m</td>
        <td align="left">设置前景色（字体色）<br>30:黑 31:红 32:绿 33:黄 34:蓝 35:紫 36:深绿 37:白</td>
    </tr>
    <tr>
        <td align="center">\033[40m ~ \033[47m</td>
        <td align="left">设置背景色<br>40:黑 41:红 42:绿 43:黄 44:蓝 45:紫 46:深绿 47:白</td>
    </tr>
    <tr>
        <td align="center">\033[nA</td>
        <td align="left">光标上移n行</td>
    </tr> 
    <tr>
        <td align="center">\033[nB</td>
        <td align="left">光标下移n行</td>
    </tr> 
    <tr>
        <td align="center">\033[nC</td>
        <td align="left">光标右移n行 </td>
    </tr>
    <tr>
        <td align="center">\033[nD</td>
        <td align="left">光标左移n行 </td>
    </tr>
    <tr>
        <td align="center">\033[y;xH</td>
        <td align="left">设置光标位置，将光标移动到y行x列的位置 </td>
    </tr>
    <tr>
        <td align="center">\033[2J</td>
        <td align="left">清屏，光标移到左上角 </td>
    </tr>
    <tr>
        <td align="center">\033[K</td>
        <td align="left">清除从光标到行尾的内容 </td>
    </tr>
    <tr>
        <td align="center">\033[s</td>
        <td align="left">保存光标位置 </td>
    </tr>
    <tr>
        <td align="center">\033[u</td>
        <td align="left">恢复光标位置 </td>
    </tr>
    <tr>
        <td align="center">\033[?25l</td>
        <td align="left">隐藏光标 </td>
    </tr>
    <tr>
        <td align="center">\033[?25h</td>
        <td align="left">显示光标</td>
    </tr>
</table>


<p><strong>【显示字符属性控制】</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[n1;n2;......m</span><br></pre></td></tr></table></figure>

<p>设定显示字符的属性状态。若有两个以上设定则以<strong>分号</strong>将代码隔开。除非重新设定，否则原设定之属性一直被保留，若想后边的不受影响，可以在结束后加一个 \033[0m 。</p>
<h2 id="3-ANSI-码应用格式"><a href="#3-ANSI-码应用格式" class="headerlink" title="3. ANSI 码应用格式"></a><font size=3>3. ANSI 码应用格式</font></h2><ul>
<li>echo</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo -e &quot;\033[颜色值m 文本&quot; # 最后加上\033[0m可以使其不影响其他字体 </span><br></pre></td></tr></table></figure>

<ul>
<li>printf</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">printf  &quot;\033[颜色值m 文本\n&quot; # 最后加上\033[0m可以使其不影响其他字体 </span><br></pre></td></tr></table></figure>

<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4.使用实例"></a><font size=3>4.使用实例</font></h2><h3 id="4-1-echo"><a href="#4-1-echo" class="headerlink" title="4.1 echo "></a><font size=3>4.1 echo </font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">echo -e &quot;\033[31m Hello World! \033[0m&quot;</span><br><span class="line">echo -e &quot;\e[31m Hello World! \e[0m&quot;</span><br><span class="line">echo -e &quot;\033[31;46m Hello World! \033[0m&quot;</span><br></pre></td></tr></table></figure>

<p>输出效果：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-03-%E8%BF%90%E7%AE%97%E7%AC%A6%E5%92%8C%E8%AF%AD%E5%8F%A5/img/image-20220214145142579.png" style="zoom:67%;" />

<h3 id="4-2-printf"><a href="#4-2-printf" class="headerlink" title="4.2 printf "></a><font size=3>4.2 printf </font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">printf &quot;\033[31m Hello World! \033[0m Hello World!\n&quot;</span><br><span class="line">printf &quot;\e[31m Hello World! \e[0m Hello World!\n&quot;</span><br><span class="line">printf &quot;\033[31;46m Hello World! \033[0m Hello World!\n&quot;</span><br></pre></td></tr></table></figure>

<p>输出效果：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-03-%E8%BF%90%E7%AE%97%E7%AC%A6%E5%92%8C%E8%AF%AD%E5%8F%A5/img/image-20220214145436032.png" style="zoom:67%;" />

<h2 id="4-3-自用变量定义"><a href="#4-3-自用变量定义" class="headerlink" title="4.3 自用变量定义"></a><font size=3>4.3 自用变量定义</font></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">BLACK=&quot;\033[1;30m&quot;</span><br><span class="line">RED=&#x27;\033[1;31m&#x27;    # 红</span><br><span class="line">GREEN=&#x27;\033[1;32m&#x27;  # 绿</span><br><span class="line">YELLOW=&#x27;\033[1;33m&#x27; # 黄</span><br><span class="line">BLUE=&#x27;\033[1;34m&#x27;   # 蓝</span><br><span class="line">PINK=&#x27;\033[1;35m&#x27;   # 紫</span><br><span class="line">CYAN=&#x27;\033[1;36m&#x27;   # 青</span><br><span class="line">WHITE=&#x27;\033[1;37m&#x27;  # 白</span><br><span class="line">CLS=&#x27;\033[0m&#x27;       # 清除颜色</span><br><span class="line"></span><br><span class="line">INFO=&quot;$&#123;GREEN&#125;[INFO]$&#123;CLS&#125;&quot;</span><br><span class="line">WARN=&quot;$&#123;YELLOW&#125;[WARN]$&#123;CLS&#125;&quot;</span><br><span class="line">ERR=&quot;$&#123;RED&#125;[ERR ]$&#123;CLS&#125;&quot;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/fbb9bfa7.html">LV02-02-shell-03-运算符和语句</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年02月21日 - 11:53</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/fbb9bfa7.html" title="LV02-02-shell-03-运算符和语句">https://sumumm.github.io/post/fbb9bfa7.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV02-编译基础</a>
              <a href="/tags/shell%E8%84%9A%E6%9C%AC/" rel="tag"><i class="fa fa-tag"></i> shell脚本</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/aef398b0.html" rel="prev" title="LV02-02-shell-04-函数和文件包含">
                  <i class="fa fa-angle-left"></i> LV02-02-shell-04-函数和文件包含
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/d8e0ae4.html" rel="next" title="LV02-02-shell-02-变量与替换">
                  LV02-02-shell-02-变量与替换 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
